package com.material.vtool.controller;

import com.google.common.collect.Maps;
import com.material.vtool.entity.BUserInfoEntity;
import com.material.vtool.entity.combinedEntity.*;
import com.material.vtool.entity.reqEntity.MaterialPageReqBean;
import com.material.vtool.entity.respEntity.RespData;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.service.MaterialTypeInfoService;
import com.material.vtool.service.UserInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 用户
 */
@RestController
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MaterialTypeInfoService materialTypeInfoService;

    /**
     * 根据用户ID获取用户信息
     * @param userId
     * @return
     */
    @GetMapping(value = "/userInfo", produces = "application/json;charset=UTF-8")
    public RespData userInfo(@RequestHeader("userId") String userId) {
        BUserInfoEntity bUserInfoEntity = userInfoService.getUserInfoByUserId(userId);
        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("userInfo", bUserInfoEntity);
        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 编辑用户
     * @param bUserInfo
     * @return
     */
    @PostMapping(value = "/updateUserInfo", produces = "application/json;charset=UTF-8")
    public RespData updateUserInfo(@RequestBody BUserInfoEntity bUserInfo) {
        userInfoService.updateUserInfo(bUserInfo);
        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 个人中心：获取用户的分享素材列表
     * @param userId 用户ID
     * @param materialPageReqBean 文章列表实体
     * @return
     */
    @PostMapping(value = "/userShareList", produces = "application/json;charset=UTF-8")
    public RespData userShareList(@RequestHeader("userId") String userId, @RequestBody MaterialPageReqBean materialPageReqBean) {
        if (materialPageReqBean == null) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Integer currentUserId = Integer.valueOf(userId);
        Integer pageNum = materialPageReqBean.getPageNum();
        Integer pageSize = materialPageReqBean.getPageSize();

        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = Integer.valueOf(1);
        }

        if (pageSize == null || pageNum.intValue() <= 0) {
            pageSize = Integer.valueOf(10);
        }

        List<CombinedUserShareMaterialEntity> list = userInfoService.getUserShareList(pageNum, pageSize, currentUserId);
        if (CollectionUtils.isNotEmpty(list)) {

            for (CombinedUserShareMaterialEntity entity : list) {
                // 获取文章的类型
                if (entity != null && entity.getMaterialTypeId() != null) {
                    Integer materialTypeId = entity.getMaterialTypeId();
                    // 查看上级类型
                    int parentTypeId = materialTypeInfoService.getMaterialParentTypeId(materialTypeId).intValue();
                    // 如果存在上级，则使用上级类型ID
                    CombinedArticleTypeIntoEntity combinedArticleTypeIntoEntity = materialTypeInfoService.getMaterialTypeById(parentTypeId > 0 ? parentTypeId : materialTypeId);

                    entity.setMaterialTypeName(combinedArticleTypeIntoEntity.getMaterialTypeName());
                    entity.setMaterialTypeLogo(combinedArticleTypeIntoEntity.getMaterialTypeLogo());
                }
            }
        }

        int totalRecord = 0;// 总记录数
        int totalPage = 0;// 总页数
        totalRecord = userInfoService.getUserShareListTotal(currentUserId);
        totalPage = totalRecord % pageSize.intValue() == 0 ? totalRecord / pageSize.intValue() : (totalRecord / pageSize.intValue()) + 1;
        PageResult pageResult = new PageResult(Integer.valueOf(totalRecord), pageSize, pageNum, Integer.valueOf(totalPage));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("pageResult", pageResult);
        resultMap.put("userShareList", list);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 个人中心：获取用户的下载列表
     * @param userId 用户ID
     * @param materialPageReqBean 文章列表实体
     * @return
     */
    @PostMapping(value = "/userDownloadList", produces = "application/json;charset=UTF-8")
    public RespData userDownloadList(@RequestHeader("userId") String userId, @RequestBody MaterialPageReqBean materialPageReqBean) {
        if (materialPageReqBean == null) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Integer currentUserId = Integer.valueOf(userId);
        Integer pageNum = materialPageReqBean.getPageNum();
        Integer pageSize = materialPageReqBean.getPageSize();

        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = Integer.valueOf(1);
        }

        if (pageSize == null || pageNum.intValue() <= 0) {
            pageSize = Integer.valueOf(10);
        }

        List<CombinedUserDownloadMaterialEntity> list = userInfoService.getUserDownloadList(pageNum, pageSize, currentUserId);
        if (CollectionUtils.isNotEmpty(list)) {

            for (CombinedUserDownloadMaterialEntity entity : list) {
                // 获取文章的类型
                if (entity != null && entity.getMaterialTypeId() != null) {
                    Integer materialTypeId = entity.getMaterialTypeId();
                    // 查看上级类型
                    int parentTypeId = materialTypeInfoService.getMaterialParentTypeId(materialTypeId).intValue();
                    // 如果存在上级，则使用上级类型ID
                    CombinedArticleTypeIntoEntity combinedArticleTypeIntoEntity = materialTypeInfoService.getMaterialTypeById(parentTypeId > 0 ? parentTypeId : materialTypeId);

                    entity.setMaterialTypeName(combinedArticleTypeIntoEntity.getMaterialTypeName());
                    entity.setMaterialTypeLogo(combinedArticleTypeIntoEntity.getMaterialTypeLogo());
                }
            }
        }

        int totalRecord = 0;// 总记录数
        int totalPage = 0;// 总页数
        totalRecord = userInfoService.getUserDownloadListTotal(currentUserId);
        totalPage = totalRecord % pageSize.intValue() == 0 ? totalRecord / pageSize.intValue() : (totalRecord / pageSize.intValue()) + 1;
        PageResult pageResult = new PageResult(Integer.valueOf(totalRecord), pageSize, pageNum, Integer.valueOf(totalPage));

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("pageResult", pageResult);
        resultMap.put("userDownloadList", list);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }
}
